import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { I18nService } from '../../core/i18n/i18n.service';
import { SkinType, RatingConfig, SwiperComponent, DialogService, DialogConfig } from 'ngx-weui';
import { MessageService } from './../../core/message.service';
import { ApiService } from 'src/app/core/api';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
	selector: 'app-Agency',
	templateUrl: './index.component.html',
	styleUrls: ['./index.component.less'],
	encapsulation: ViewEncapsulation.None,
})
export class AgencyComponent implements OnInit {
	@ViewChild('swiper', { static: true }) swiper: SwiperComponent;

	del: any;
	id: string;
	isConcern: boolean = false;
	imgs: Array<any>;
	showLogin = false;
	token: string = null;
	log: string = '';
	rate: any = [{ url: '../../../assets/icons/off.png' }, { url: '../../../assets/icons/off.png' }, { url: '../../../assets/icons/off.png' }, { url: '../../../assets/icons/off.png' }, { url: '../../../assets/icons/off.png' }];
	serviceStatus: string;
	serviceTime: string;
	customIconsAndClassCog: RatingConfig = {
		cls: 'rating',
		stateOff: 'off',
		stateOn: 'on',
	};
	config: DialogConfig = {
		title: '价格说明',
		content: '弹窗内容，告知当前状态、信息和解决方法，服务费用支付后不可退款！',
		// cancel: '',
		confirm: '已知晓',
	};
	options: any = {
		init: false,
		observer: true,
		observeParents: true,
		effect: 'coverflow',
		slideShadows: false,
		pagination: {
			el: '.swiper-pagination',
		  },
	};
	iData: any;
	constructor(private i: I18nService, private api: ApiService, private route: ActivatedRoute, private router: Router, private srv: DialogService) {
		this.i.get(['AGENCY-Page.price-indication', 'AGENCY-Page.Service-Items', 'AGENCY-Page.price-text', 'cancel', 'AGENCY-Page.confirm', 'AGENCY-Page.serviceStatus-Y', 'AGENCY-Page.serviceStatus-N', 'MSG.YGZ', 'MSG.YQXGZ', 'AGENCY-Page.serviceTime-N']).subscribe(res => {
			this.iData = res;
			this.config = {
				title: res['AGENCY-Page.price-indication'],
				content: res['AGENCY-Page.price-text'],
				cancel: undefined,
				confirm: res['AGENCY-Page.confirm'],
			};
		});
	}
	ngOnInit() {
		if (localStorage.getItem('user')) {
			this.token = JSON.parse(localStorage.getItem('user'));
		}
		this.id = this.route.snapshot.queryParams['travelProductId'];
		this.del = this.route.snapshot.data.agency.del;
		this.imgs = this.route.snapshot.data.agency.imgs;
		this.rate = this.route.snapshot.data.agency.rate;
		this.serviceStatus = this.route.snapshot.data.agency.serviceStatus == 'Y' ? this.iData['AGENCY-Page.serviceStatus-Y'] : this.iData['AGENCY-Page.serviceStatus-N'];
		this.serviceTime = this.route.snapshot.data.agency.serviceTime ? this.route.snapshot.data.agency.serviceTime : this.iData['AGENCY-Page.serviceTime-N'];
		setTimeout(() => {
			this.swiper.swiper.init();
		});
	}

	onShowOfHtml() {
		this.srv.show(this.config).subscribe((res: any) => {
			console.log(res);
		});
		return false;
	}

	onStore(item) {
		if (item.isFollowed == 'Y') {
			item.isFollowed = 'N';
		} else {
			item.isFollowed = 'Y';
		}
		this.api.post('用户关注导游接口', { guideId: item.id, isFollowed: item.isFollowed }).then((e: any) => {
			item.isFollowed == 'Y' ? this.api.msgSuccess(this.iData['MSG.YGZ']) : this.api.msgSuccess(this.iData['MSG.YQXGZ']);
		});
	}
	onSel(item) {
		console.log('this.del.id',this.del['travelAgencyForUserView'].id)
		this.router.navigate(['/guide'], {
			queryParams: {
				travelAgencyId: this.del['travelAgencyForUserView'].id,
				travelGuideId: item.id,
				backParams:this.route.snapshot.queryParams,
				type: 1,
			},
		});
	}
	loginSuccess(){
		if (localStorage.getItem('user')) {
			this.token = JSON.parse(localStorage.getItem('user'));
		}
	}
	toCreateOrder() {
		this.api.post('点击预约计数');
		this.router.navigate(['/order'], {
			queryParams: {
				travelAgencyId: this.del.travelAgencyForUserView.id,
				reservationFee: this.del.travelAgencyForUserView.reservationFee,
				isLXS: 'gzAgency',
			},
		});
	}
	openLogin() {
		this.showLogin = true;
	}

	onChangeConcern() {
		if (this.del.isFollowed == 'Y') {
			this.del.isFollowed = 'N';
		} else {
			this.del.isFollowed = 'Y';
		}
		this.api.post('旅行社关注', { agencyId: this.del.travelAgencyForUserView.id, isFollowed: this.del.isFollowed }).then((e: any) => {
			this.del.isFollowed == 'Y' ? this.api.msgSuccess('已收藏') : this.api.msgSuccess('取消收藏');
		});
	}
}
